package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.domain.DesignScheme;
import com.ruoyi.system.domain.DesignSchemeImage;
import com.ruoyi.system.mapper.DesignSchemeImageMapper;
import com.ruoyi.system.mapper.DesignSchemeMapper;
import com.ruoyi.system.service.DesignSchemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * 设计方案主表Service实现类
 */
@Service
public class DesignSchemeServiceImpl extends ServiceImpl<DesignSchemeMapper, DesignScheme> implements DesignSchemeService {

    @Autowired
    private DesignSchemeImageMapper imageMapper;

    /**
     * 根据基础设计ID查询所有方案
     */
    @Override
    public List<DesignScheme> selectByDesignBaseId(Long designBaseId) {
        List<DesignScheme> schemes = baseMapper.selectByDesignBaseId(designBaseId);
        // 加载每个方案的图片
        for (DesignScheme scheme : schemes) {
            List<DesignSchemeImage> images = imageMapper.selectBySchemeId(scheme.getId());
            scheme.setImages(images);
        }
        return schemes;
    }

    @Override
    public DesignScheme getDesignSchemeByOnlineId(String onlineId) {
        return baseMapper.selectOne(
                new QueryWrapper<DesignScheme>()
                        .eq("online_design_id", onlineId)
                        .last(" limit 1"));
    }

    /**
     * 保存设计方案及关联图片
     */
    @Override
    @Transactional
    public boolean saveSchemeWithImages(DesignScheme designScheme) {
        // 保存方案主表
        if (!save(designScheme)) {
            return false;
        }

        // 保存图片
        List<DesignSchemeImage> images = designScheme.getImages();
        if (images != null && !images.isEmpty()) {
            for (DesignSchemeImage image : images) {
                image.setSchemeId(designScheme.getId());
                imageMapper.insert(image);
            }
        }

        return true;
    }

    /**
     * 更新设计方案及关联图片
     */
    @Override
    @Transactional
    public boolean updateSchemeWithImages(DesignScheme designScheme) {
        // 更新方案主表
        if (!updateById(designScheme)) {
            return false;
        }

        // 删除原有图片
        QueryWrapper<DesignSchemeImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheme_id", designScheme.getId());
        imageMapper.delete(queryWrapper);

        // 保存新图片
        List<DesignSchemeImage> images = designScheme.getImages();
        if (images != null && !images.isEmpty()) {
            for (DesignSchemeImage image : images) {
                image.setSchemeId(designScheme.getId());
                imageMapper.insert(image);
            }
        }

        return true;
    }

    /**
     * 根据方案ID删除方案及关联图片
     */
    @Override
    @Transactional
    public boolean removeSchemeWithImages(Long id) {
        // 删除关联图片
        QueryWrapper<DesignSchemeImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("scheme_id", id);
        imageMapper.delete(queryWrapper);

        // 删除方案主表
        return removeById(id);
    }
}
